package com.whfp.coky.ui.task.vm.fluor

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.raizlabs.android.dbflow.sql.language.SQLite
import com.whfp.coky.api.response.BaseResult
import com.whfp.coky.api.response.ListBean
import com.whfp.coky.databinding.RecycleTitleViewBinding
import com.whfp.coky.db.DbFlowTools
import com.whfp.coky.db.dao.ProjectSettingDao
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.base.BaseRecycleAct
import com.whfp.coky.ui.base.vm.RecycleVM
import com.whfp.coky.utils.LogUtil

/**
 *
 *
 * created by coky on 2022/3/1
 */
class ThresholdListVM: RecycleVM<RecycleTitleViewBinding, ProjectSettingDao>() {

    var dbDatasLD = MutableLiveData<MutableList<ProjectSettingDao>>()

    override fun getStatusLayout() = vBinding.statusLayout

    override fun buildRequest() {
        onRefresh()
    }

    override suspend fun httpBlock(page: Int): BaseResult<ListBean<ProjectSettingDao>> {
//        return BaseResult("success",Constants.HTTP_OK,DbflowTool.querySettingsByPage(page))
        return BaseResult()
    }

    override fun observe(activity: BaseActivity<*, *>, owner: LifecycleOwner) {
        super.observe(activity, owner)
        dbDatasLD.observe(activity, Observer {
            (activity as BaseRecycleAct<*, *, *>).httpFinshed()
            handleDatas(it)
            isLastPage = it.size != Constants.PAGE_SIZE

            LogUtil.e("isLastPage == null??${it.size == Constants.PAGE_SIZE}")
        })
    }



    override fun httpDatas(page: Int) {
//        dbDatasLD.value = DbflowTool.querySettingsByPage(page)
        LogUtil.e("size.....${SQLite.select().from(ProjectSettingDao::class.java).queryList().size}")
        LogUtil.e("get0..thresholdInfo...${SQLite.select().from(ProjectSettingDao::class.java).queryList().get(0).thresholdInfo}")
        dbDatasLD.value = DbFlowTools.querySettingsByPage(page)
    }

}